package me.zingon.wxbus.service;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zingon.wxbus.dao.LineDao;
import me.zingon.wxbus.dao.LineStationDao;
import me.zingon.wxbus.dao.StationDao;
import me.zingon.wxbus.model.Line;
import me.zingon.wxbus.model.LineStation;
import me.zingon.wxbus.model.Station;
import me.zingon.wxbus.util.LineUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class LineService {

    @Autowired
    LineUtil lineUtil;

    @Autowired
    LineDao lineDao;

    @Autowired
    StationDao stationDao;

    @Autowired
    LineStationDao lineStationDao;

    public void loadLine(String json,String rtSign){
        List<Line> lines = lineUtil.loadLine(json,rtSign);
        lines.stream().forEach(line -> lineDao.insert(line));
    }


    public void loadStaion() {
        List<Line> lines=lineDao.selectList(null);
        lines.stream().forEach(line -> {
            JSONObject json=new JSONObject();
            json.put("companyType",line.getCompanyType())
                    .put("gprsId",line.getGprsId())
                    .put("segmentID",line.getSegmentId());
            List<Station> stations = lineUtil.loadStation(json.toString(),line.getRtSign());
            for(int i=0;i<stations.size();i++){
                try {
                    stationDao.insert(stations.get(i));
                }catch (Exception e){
                    System.out.println(stations.get(i).getStationName()+" 重复");
                }
                LineStation ls=new LineStation();
                ls.setLineId(line.getSegmentId());
                ls.setStationId(stations.get(i).getStationId());
                ls.setOrderno(i);
                lineStationDao.insert(ls);
            }
        });
    }

    public Line getByName(String name) {
        QueryWrapper<Line> param=new QueryWrapper<>();
        param.eq("name",name);
        return lineDao.selectOne(param);
    }
}
